﻿using UnityEngine;

namespace Game
{
    public abstract class SceneObjectEntity : GameObjectContainerEntity, ISceneObjectEntity
    {
        protected abstract void UpdateSortingLayer(int layerId);
        protected abstract void UpdateSortingOrder(int order);

        private int _id;
        private SceneObjectEntityType _entityType;
        private int _sortingOrder = 0;

        public SceneObjectEntity(int id, SceneObjectEntityType entityType)
        {
            this._id = id;
            this._entityType = entityType;
        }

        public int GetId()
        {
            return _id;
        }

        public SceneObjectEntityType GetEntityType()
        {
            return _entityType;
        }

        public void SetSortingOrder(int order)
        {
            _sortingOrder = order;
            if (_isCompleted)
            {
                UpdateSortingOrder(_sortingOrder);
            }
        }

        public abstract void SetFlipX(bool flip);

        public abstract void SetFlipY(bool flip);

        public abstract bool GetFlipX();

        public abstract bool GetFlipY();
        
        public virtual void Update(float deltaTime)
        {
            for (int i = 0; i < _componentList.Count; ++i)
            {
                IComponent component = _componentList[i];
                if (component is ISceneComponent)
                {
                    (component as ISceneComponent).Update(deltaTime);
                }
            }
        }
        
        protected override void OnInstantiateCompleted()
        {
            base.OnInstantiateCompleted();
            UpdateSortingLayer(Const.SortingLayerID.Scene);
            UpdateSortingOrder(_sortingOrder);
        }
    }
}